const { Router } = require("express");
const User = require("../model/Concern");
const Concern = require("../model/Concern");

const router = new Router();
router.use(require('./auths.js'));

// 查询是否已关注
router.post("/findConcern", async (req, res) => {
    const user_id = req.userInfo._id;
    // console.log(req.body);
    let { user_idb } = req.body;
    // console.log(user_idb, user_id)
    const data = await Concern.find({ user_id, user_idb })
    if (data.length == 0) {
        res.status(200).json({ message: "未关注", concern: false })
        return
    }
    res.status(200).json({ message: "已关注", concern: true })
})

// 加关注
router.post("/addConcern", async (req, res) => {
    const user_id = req.userInfo._id;
    let { user_idb, type } = req.body
    const data = await Concern.create({ user_id, user_idb, type })
    res.status(200).json({ message: "关注成功", data, concern: true })
})

// 取消关注
router.post("/cancelConcern", async (req, res) => {
    const user_id = req.userInfo._id;
    let { user_idb } = req.body
    const data = await Concern.deleteOne({ user_id, user_idb })
    res.status(200).json({ message: "取消关注", data, concern: false })
})

module.exports = router;